package com.fy56.platform.goodsTracking.commons.security;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5 {
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * 转换字节数组为十六进制字符串
     *
     * @param 字节数组
     * @return 十六进制字符串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    /**
     * 将一个字节转化成十六进制形式的字符串
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }


    public static String getMd5Str(String filename) {
        String md5str = "";
        try {
            byte[] filebytes = ReadFileTool.readfileByBytes(filename);
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] mymd5 = md.digest(filebytes);
            //将得到的字节数组变成字符串返回
            md5str = MD5.byteArrayToHexString(mymd5);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return md5str;
    }

    /**
     * MD5 32位加密方法一 小写
     *
     * @param s
     * @return
     */
    public final static String get32MD5(String s) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] strTemp = s.getBytes();
//使用MD5创建MessageDigest对象
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte b = md[i];
//System.out.println((int)b);
//将没个数(int)b进行双字节加密
                str[k++] = hexDigits[b >> 4 & 0xf];
                str[k++] = hexDigits[b & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * return a 16 bit byte[] array
     *
     * @param message
     * @return
     */
    public static String digest(String message) {
        String outString = null;
        byte[] digests = null;

        try {
            digests = encode(message, "GBK");
            //System.out.println("digests.length : " + digests.length);
            //System.out.println("new String(digests).length() : " + new String(digests, "UTF-8").length());
            //System.out.println("new String(digests) : " + new String(digests));

            outString = StringUtil.valueOfHex(digests);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return outString;
    }

    public static byte[] encode(String content, String encoding)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        // 确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        // 加密后的内容
        byte[] md5Contnet = md5.digest(content.getBytes(encoding));

        return md5Contnet;
    }

    public static byte[] encode(byte[] content)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        // 确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        // 加密后的内容
        byte[] md5Contnet = md5.digest(content);

        return md5Contnet;
    }

    public static void main(String args[]) {
        String resultString = MD5.digest("111111");
        System.out.println(resultString.toUpperCase());
    }

}
